<?php
namespace ZfcUser\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
use Zend\Paginator\Paginator;
class TuserTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function resetpassword($email,$password)
	{
		$this->tableGateway->update(array("password"=>$password),array('email_id' => $email));
		return $this->tableGateway->lastInsertValue;
	}
	public function addUser($user,$token)
    {
		$password=md5($token);
		$data = array(
			'address'				=> 	null,  		
			'city'  				=> 	$user['city'],
			'state'  				=> 	$user['state'],
			'create_time'			=>	date('Y-m-d H:i:s'), 
			'dob'					=> 	null, 	
			'email_id'				=> 	$user['emailId'],  	
			'email_validation'		=> 	$user['emailId'],   
			'father_name'			=> 	null,
			'first_name'			=> 	$user['name'],  
            'gender'				=>	null,
            'ip'					=>	$_SERVER['REMOTE_ADDR'],
            'last_name'				=>	null,
            'login_id'				=>	null,
            'mobile'				=>	$user['mobileNo'],
            'password'				=>	$password,
            'user_status'			=>	0,
            'surname'				=>  null,
            'validation_code'		=>  null,
            'validation_time'		=>  null,
			
		);
		//echo '<pre>'; print_r($data); exit;
		$this->tableGateway->insert($data);
		
		$select = $this->tableGateway->getSql()->select()
				->where('email_id="'.$user['emailId'].'"')
				->where('password="'.$password.'"');
				
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->current();
		return $row;
    }
	public function checkEmail($email)
    {	
	 $select = $this->tableGateway->getSql()->select()			
			->where('email_id="'.$email.'"');					 
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();
		return $row;		
	}
	public function activeUser($id)
    {	
	 $data = array(
				'user_id'      =>	$id,
				'user_status'  =>	1,
			);
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$updateuserid;			
	}
	public function getpassword($pwd,$userid){ 
		$pwd=md5($pwd);
		$select = $this->tableGateway->getSql()->select();
		$select->where('password="'.$pwd.'"');
		$select->where('user_id="'.$userid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();			
		return $row;
	}
	public function changepwd($pwd,$userid){
		$password=md5($pwd);
		$data = array(
				'user_id'       =>$userid,
				'password'      =>$password,
				);
		$changepassword=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$changepassword;			
	}
	public function checkLogin($user)
    {		
		$password=md5($user['userPassword']);
		$select = $this->tableGateway->getSql()->select()
				->where('email_id="'.$user['userEmail'].'"')
				->where('password="'.$password.'"');
		$resultSet = $this->tableGateway->selectWith($select);		
        $row = $resultSet->current();		
        return $row;
	}
}